

package com.hazelcast.client.impl.protocol.task.executorservice;

import com.hazelcast.client.impl.protocol.ClientMessage;
import com.hazelcast.client.impl.protocol.codec.ExecutorServiceCancelOnMemberCodec;
import com.hazelcast.client.impl.protocol.task.AbstractTargetMessageTask;
import com.hazelcast.executor.impl.DistributedExecutorService;
import com.hazelcast.executor.impl.operations.CancellationOperation;
import com.hazelcast.instance.impl.Node;
import com.hazelcast.internal.nio.Connection;
import com.hazelcast.security.SecurityInterceptorConstants;
import com.hazelcast.security.permission.ActionConstants;
import com.hazelcast.security.permission.ExecutorServicePermission;
import com.hazelcast.spi.impl.operationservice.Operation;

import java.security.Permission;
import java.util.UUID;

public class ExecutorServiceCancelOnAddressMessageTask extends AbstractTargetMessageTask<ExecutorServiceCancelOnMemberCodec.RequestParameters> {

    public ExecutorServiceCancelOnAddressMessageTask(ClientMessage clientMessage, Node node, Connection connection) {
        super(clientMessage, node, connection);
    }

    @Override
    protected Operation prepareOperation() {
        return new CancellationOperation(parameters.uuid, parameters.interrupt);
    }

    @Override
    protected UUID getTargetUuid() {
        return parameters.memberUUID;
    }

    @Override
    protected ExecutorServiceCancelOnMemberCodec.RequestParameters decodeClientMessage(ClientMessage clientMessage) {
        return ExecutorServiceCancelOnMemberCodec.decodeRequest(clientMessage);
    }

    @Override
    protected ClientMessage encodeResponse(Object response) {
        return ExecutorServiceCancelOnMemberCodec.encodeResponse((Boolean) response);
    }

    @Override
    public String getDistributedObjectName() {
        DistributedExecutorService service = getService(getServiceName());
        return service.getName(parameters.uuid);
    }

    @Override
    public String getServiceName() {
        return DistributedExecutorService.SERVICE_NAME;
    }

    @Override
    public Permission getRequiredPermission() {
        String name = getDistributedObjectName();
        if (name == null) {
            // The permission constructor expects a non-null name.
            name = ExecutorServicePermission.EMPTY_EXECUTOR_NAME;
        }

        return new ExecutorServicePermission(name, ActionConstants.ACTION_MODIFY);
    }

    @Override
    public String getMethodName() {
        return SecurityInterceptorConstants.CANCEL;
    }

    @Override
    public Object[] getParameters() {
        return null;
    }
}
